package com.dome.testgallery;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.dome.testgallery.adapter.GalleryIndexAdapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TestActivity extends Activity{

	private Gallery myGallery;
	private LinearLayout layout;
	private TextView text;
	
	private String[] txtStr = {"第一张图片","第二张图片","第三张图片"};
	private List<ImageView> images = new ArrayList<ImageView>();
	
	private Activity activity;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_test);
		activity = this;
		
		initView();
		addEvn();
	}

	private void initView() {
		myGallery = (DetailGallery) findViewById(R.id.myGallery);
		layout = (LinearLayout) findViewById(R.id.linear);
		text = (TextView) findViewById(R.id.text);
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(R.drawable.banner1);
		list.add(R.drawable.banner2);
		list.add(R.drawable.banner3);
		GalleryIndexAdapter adapter = new GalleryIndexAdapter(list, activity);
		myGallery.setAdapter(adapter);
		
		for(int i=0; i<list.size(); i++){
			ImageView image = new ImageView(activity);
			image.setBackgroundResource(R.drawable.gallery_radio_off);
			int wh = Tool.dp2px(activity, 10);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(wh, wh);
			params.setMargins(4, 0, 4, 0);
			image.setLayoutParams(params);
			images.add(image);
			layout.addView(image);
		}
		
		text.setText(txtStr[0]);
		images.get(0).setBackgroundResource(R.drawable.gallery_radio_on);
	}
	
	private void addEvn() {
		myGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				setImageBackground();
				text.setText(txtStr[position % txtStr.length]);
				images.get(position % images.size()).setBackgroundResource(R.drawable.gallery_radio_on);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}
	
	protected void onResume() {
		autogallery();
		super.onResume();
	};

	private void autogallery() {
		/* 设置定时器，每5秒自动切换展示图 */
		Timer time = new Timer();
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				Message m = new Message();
				handler_gallery.sendMessage(m);
			}
		};
		time.schedule(task, 8000, 5000);
	}
	
	private Handler handler_gallery = new Handler() {
		public void handleMessage(android.os.Message msg) {
			/* 自定义屏幕按下的动作 */
			MotionEvent e1 = MotionEvent.obtain(SystemClock.uptimeMillis(),
					SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
					89.333336f, 265.33334f, 0);
			/* 自定义屏幕放开的动作 */
			MotionEvent e2 = MotionEvent.obtain(SystemClock.uptimeMillis(),
					SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
					300.0f, 238.00003f, 0);
			
			myGallery.onFling(e2, e1, -800, 0);
			/* 给gallery添加按下和放开的动作，实现自动滑动 */
			super.handleMessage(msg);
		};
	};

	private void setImageBackground(){
		for(int i =0; i<images.size(); i++){
			images.get(i).setBackgroundResource(R.drawable.gallery_radio_off);
		}
	}
}
